<script setup>


import {usePayTime} from "@/hooks/index.js";
import {ref} from "vue";
import {buyGoodsNow} from "@/api/buyGoods.js";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";
import {useOrderInfoStore} from "@/stores/index.js";

const { timeText } = usePayTime()
const platform = ref(0)
const router = useRouter()
// 支付
// 拿到商品数据
const orderInfoStore = useOrderInfoStore()
const goods = orderInfoStore.goodsInfo
const loading = ref(false)
const PayImmediately = async () => {
  loading.value = true
  await buyGoodsNow(
      // 购买人id
      orderInfoStore.orderInfo.userId,
      // 地址id
      orderInfoStore.addrInfo.id,
      // 发货人id
      1,
      // 商品id
      goods.goods_id,
      // 商品数量
      goods.count,
      // 总价格
      goods.totalPrice,
      // 订单状态
      1
  )
  loading.value = false
  ElMessage.success('购买成功')
  router.push('/orderdisplay')
}
</script>

<template>
  <div class="xtx-pay-page">
    <div class="container">
      <div class="hr"></div>
      <!-- 付款信息 -->
      <div class="pay-info">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付。</p>
          <p>支付还剩 <span>{{ timeText }}</span>, 超时后将取消订单</p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ goods.totalPrice }}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <a
              @click="platform = 1"
              class="btn wx"
              :class="{ active: platform === 1 }"
              href="javascript:;">
            <b :class="platform === 1 ? 'icon' : null"></b>
          </a>
          <a
              @click="platform = 2"
              class="btn alipay"
              :class="{ active: platform === 2 }"
              href="javascript:;">
            <b :class="platform === 2 ? 'icon' : null"></b>
          </a>
        </div>
        <div class="pay-btn">
          <el-button
              :disabled="platform === 0 ? true : false"
              v-loading.fullscreen="loading"
              @click="PayImmediately"
              class="pay"
              size="large"
          type="primary"
          plain
          >立即支付</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.hr {
  height: 18px;
}
.pay-info {
  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;
  .icon {
    font-size: 80px;
    color: #1dc779;
  }
  .tip {
    padding-left: 10px;
    flex: 1;
    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }
      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }
  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }
      &:last-child {
        color: #d62222;
        font-size: 20px;
      }
    }
  }
}
.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;

  .pay-btn {
    margin-top: 35px;
    display: flex;
    justify-content: flex-end;
    .pay {
      margin-right: 130px;
    }
  }


  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;
    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }
  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;

    .icon {
      position: relative;
      top: 37px;
      float: right;
      display: block;
      width: 12px;
      height: 12px;
      overflow: hidden;
      background: url("@/assets/selected-icon.png") no-repeat;
    }

    &.active,
    &:hover {
      border-color: #d62222;
    }
    &.alipay {
      background: url(https://sky-takeout-ximeng.oss-cn-hangzhou.aliyuncs.com/zfb.png) no-repeat center / contain;
    }
    &.wx {
      background: url(https://sky-takeout-ximeng.oss-cn-hangzhou.aliyuncs.com/wx.jpg) no-repeat center / contain;
    }
  }
}
</style>
